PRO CHECK_GRS_MATCH, havesave, angsep
  
  COMMON FFORE_BLOCK,n,rb3,R0,d,R,Z,tau,f_data,sig_f,corr,farlist,do_Tfit,$
     lpstr,rho_hi,rho_h2,rho_star
  
  ;;======================================
  ;; SELECT WHETHER WE HAVE THE GRS INFO SAVED
  IF n_elements(havesave) EQ 0 THEN havesave = 0b
  IF n_elements(angsep) EQ 0 THEN angsep = 4.
  
  undefine,rb3
  
  IF ~ havesave THEN fill_in_rb3_grs,ANGSEP=angsep
  
  
  
  
  ;;==================================================================
  ;; MAKE ALL THE PLOTS!!!!
  
  ;; Clear out rb3 sources NOT well constrained
  cind = where(rb3.constrain EQ 1, nc)
  print,string(nc,n_elements(rb3),format="('N constrained: ',I0,' of ',I0)")
  rb3 = rb3[cind]
  
  ;; Just select sources associated with GRS objects
  pind = where(rb3.grs EQ 1, np)
  print,string(np,angsep,format=$
               "('GRS-associated objects: ',I0,' at angsep = ',I0,' arcmin')")
  rb3 = rb3[pind]
  
  
  
  ;;===========================================================================
  ;; PLOT 1 -- Distance vs. Distance Plot
  ;;=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  
  myps,'./irdc_dist_model/analysis_plots/grs_model_compare.eps',xsize=5
  
  range = [0,14]
  
  plot,rb3.grs_dist,rb3.fitdist,psym=8,xtit='GRS Distance [kpc]'$
       ,ytit='IRDC Modeling Distance [kpc]',/isotropic,$
       xr=range,/xst,yr=range,/yst
  
  ;; Plot the 'alley'
  plots,0,1
  plots,range[1]-1,range[1],/cont,linestyle=2,color=cgColor('BLK5')
  plots,1,0
  plots,range[1],range[1]-1,/cont,linestyle=2,color=cgColor('BLK5')
  
  ind = where(rb3.grs_match EQ 0, nbad)
  al_legend,['N!dmismatch!n = '+string(nbad,format="(I0)"),$
             'N!dtotal!n = '+string(np,format="(I0)")],box=0,/top,/left,$
            charsize=0.8
  
  myps,/done
  
  
  
  ;;===========================================================================
  ;; PLOT 2 -- Crazy set of plots about matching width, etc.
  ;;=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  
  myps,'./irdc_dist_model/analysis_plots/grs_model_compare2.eps',$
       xsize=10,ysize=9
  multiplot,[2,2],xgap=0.06,ygap=0.04,/doxaxis,/doyaxis
  
  ;; Make bar chart
  step = 0.1d
  hstep = step / 2.d
  ni = fix(round(1.d / step))
  nsucc = lonarr(ni)
  xp    = fltarr(ni)
  yp    = fltarr(ni)
  yp2   = fltarr(ni)
  
  FOR i=0, ni-1 DO BEGIN
     np = step + i*step
     ind = WHERE(rb3.morph_np LE np AND rb3.morph_np GT np - step, nind)
     nind2 = 0
     IF nind GT 0 THEN BEGIN
        bar = rb3[ind]
        ind2 = WHERE(bar.grs_match EQ 1, nind2)
        yp[i] = (nind NE 0) ? float(nind2)/float(nind) : 0.
        yp2[i] = float(nind)
     ENDIF
     xp[i] = np - hstep
  ENDFOR
  
  ;; Extend histograms so they go to y=0 at both ends
  xp  = [0.-hstep,xp,1.+hstep]
  yp  = [0,yp,0]
  yp2 = [0,yp2,0]

  
  ;; Plot the histogram
  plot,xp,yp,psym=10,xtit='Morphological Matching P!dnear!n',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       xr=[0,1],/xst,thick=4
  axis,yaxis=1,yr=[0,50],ytit='N per '+string(step,format="(F0.1)")+' bin',/save
  oplot,xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6
  
  multiplot,/doxaxis,/doyaxis
  
   
  ;; Make bar chart
  ;; SR_BARS, array, minval, maxval, step, xp, yp, yp2
  minval = 0.d
  maxval = 7.d
  step   = 0.5d
  sr_bars, rb3.morph_width/1.d3, rb3.grs_match, minval, maxval, step, $
           xp, yp, yp2
  
  
  ;; PLOT
  infty = cgSymbol('infinity')
  xnames=[string(indgen(11),format="(I0)"),infty]
  
  plot,xp,yp,psym=10,xtit='Morphological Matching Width [kpc]',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       thick=4,xr=[0,6],/xst,$  ;xtickname=xnames,xticks=11,xminor=2,$
       tit='Gaussian Fit Sigma'
  axis,yaxis=1,yr=[0,100],ytit='N per '+string(step,format="(F0.1)")+$
       ' kpc bin',/save,/yst
  oplot,xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6
  
  multiplot,/doxaxis,/doyaxis
  ;; Make bar chart
  ;; SR_BARS, array, minval, maxval, step, xp, yp, yp2
  minval = 0.d
  maxval = 10.d
  step   = 1.0d
  sr_bars, rb3.morph_fwhm/1.d3, rb3.grs_match, minval, maxval, step, $
           xp, yp, yp2
  
  ;; PLOT
  plot,xp,yp,psym=10,xtit='Morphological Matching FWHM [kpc]',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       thick=4,xr=[0,maxval],/xst,tit='FWHM'
  axis,yaxis=1,yr=[0,100],ytit='N per '+string(step,format="(F0.1)")+$
       ' kpc bin',/save,/yst
  oplot,xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6
  
  multiplot,/doxaxis,/doyaxis
  
  plot,rb3.morph_width/1.d3,rb3.morph_fwhm/1.d3,psym=8,$
       xtit='Gaussian Sigma [kpc]',ytit='FWHM [kpc]',/nodata
  
  xarr = findgen(100)*!x.crange[1]
  yarr = xarr * sqrt(8.d * alog(2.))
  oplot,xarr,yarr,linestyle=3,thick=5,color=cgColor('Red5')
  oplot,rb3.morph_width/1.d3,rb3.morph_fwhm/1.d3,psym=8
  
  myps,/done,/mp
  
  
  
  ;;===========================================================================
  ;; PLOT 3 -- GRS success vs. BGPS flux & IRDC contrast
  ;;=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  
  myps,'./irdc_dist_model/analysis_plots/grs_model_compare3.eps',$
       xsize=10,ysize=4.5
  multiplot,[2,1],xgap=0.06,/doxaxis,/doyaxis
  
  junk = where(rb3.grs_match EQ 1, nyes)
  print,'N TOT  :',n_elements(rb3),'  N BAD: ',n_elements(rb3)-nyes
  print,'PLOT 3: Overall Success Rate = '+string(float(nyes)/n_elements(rb3)*$
                                                 100.,format="(F0.1,'%')")
  
  ;; Make bar chart -- IRDC CONTRAST
  ;; SR_BARS, array, minval, maxval, step, xp, yp, yp2
  minval = 0.d
  maxval = 0.5d
  step   = 0.05d
  sr_bars, rb3.c_meas, rb3.grs_match, minval, maxval, step, $
           xp, yp, yp2
  
  ;; PLOT
  plot,xp,yp,psym=10,xtit='IRDC Contrast',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       thick=4,xr=[0,maxval],/xst
  axis,yaxis=1,yr=[0,100],ytit='N per '+string(step,format="(F0.2)")+' bin',$
       /save,/yst
  oplot,xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6

  multiplot,/doxaxis,/doyaxis
  
  
  ;; Make bar chart -- BGPS FLUX DENSITY
  ;; SR_BARS, array, minval, maxval, step, xp, yp, yp2
  minval = -1.4d
  maxval = 0.5d
  sr_bars, alog10(rb3.s_peak/1.d3), rb3.grs_match, minval, maxval, 0.1d, $
           xp, yp, yp2
  
  ;; PLOT
  plot,10.^xp,yp,psym=10,xtit='BGPS 40" Flux Density [Jy]',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       thick=4,xr=10.^[minval,maxval],/xst,/xlog
  axis,yaxis=1,yr=[0,100],ytit='N per log(S)='+string(step,format="(F0.1)")+$
       ' bin',/save,/yst
  oplot,10.^xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6
  
  myps,/done,/mp
  
  
  ;;===========================================================================
  ;; PLOT 5 -- Histogram as function of GLON
  ;;=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  
  myps,'./irdc_dist_model/analysis_plots/grs_model_compare5.eps',$
       xsize=10,ysize=4.5
  multiplot,[2,1],xgap=0.06,/doxaxis,/doyaxis
  
  ;; print,'PLOT 5a'
  ;; Make bar chart -- GLON (ALL)
  ;; SR_BARS, array, minval, maxval, step, xp, yp, yp2
  minval = 20.d
  maxval = 70.d
  step   = 5.d
  sr_bars, rb3.l, rb3.grs_match, minval, maxval, step, $
           xp, yp, yp2
  
  
  ;; PLOT
  plot,xp,yp,psym=10,xtit='GLON [deg]',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       thick=4,xr=[maxval,minval],/xst,yr=[0,1],tit='All'
  axis,yaxis=1,yr=[0,100],ytit='N per '+string(step,format="(F0.2)")+$
       ' deg bin',/save,/yst
  oplot,xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6
  
  
  multiplot,/doxaxis,/doyaxis
  
  ;; print,'PLOT 5b'
  ;; Make bar chart -- GLON (C>= 0.1)
  
  ic = WHERE(rb3.c_meas GE 0.1, nic)
  fullrb3 = rb3
  rb3 = rb3[ic]
  
  ;; SR_BARS, array, minval, maxval, step, xp, yp, yp2
  ;; minval = 20.d
  ;; maxval = 70.d
  ;; step   = 5.d
  sr_bars, rb3.l, rb3.grs_match, minval, maxval, step, $
           xp, yp, yp2
  
  ;; PLOT
  geq = cgSymbol('geq')
  
  plot,xp,yp,psym=10,xtit='GLON [deg]',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       thick=4,xr=[maxval,minval],/xst,yr=[0,1],tit='C '+geq+' 0.1'
  axis,yaxis=1,yr=[0,100],ytit='N per '+string(step,format="(F0.2)")+$
       ' deg bin',/save,/yst
  oplot,xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6
  
  
  rb3 = fullrb3
  
  myps,/done,/mp
  
  
  ;;===========================================================================
  ;; PLOT 6 -- Histogram as function of GLAT
  ;;=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  
  myps,'./irdc_dist_model/analysis_plots/grs_model_compare6.eps',$
       xsize=10,ysize=4.5
  multiplot,[2,1],xgap=0.06,/doxaxis,/doyaxis
  
  ;; Make bar chart -- GLAT (ALL)
  ;; SR_BARS, array, minval, maxval, step, xp, yp, yp2
  minval = -1.0d
  maxval = 1.0d
  step   = 0.2d
  sr_bars, rb3.b, rb3.grs_match, minval, maxval, step, $
           xp, yp, yp2
  
  ;; PLOT
  plot,xp,yp,psym=10,xtit='GLAT [deg]',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       thick=4,xr=[maxval,minval],/xst,yr=[0,1],tit='All'
  axis,yaxis=1,yr=[0,100],ytit='N per '+string(step,format="(F0.2)")+$
       ' deg bin',/save,/yst
  oplot,xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6
  
  multiplot,/doxaxis,/doyaxis
  
  ;; Make bar chart -- GLAT (C >= 0.1)
  ic = WHERE(rb3.c_meas GE 0.1, nic)
  fullrb3 = rb3
  rb3 = rb3[ic]
  
  ;; SR_BARS, array, minval, maxval, step, xp, yp, yp2
  ;; minval = -1.0d
  ;; maxval = 1.0d
  ;; step   = 0.2d
  sr_bars, rb3.b, rb3.grs_match, minval, maxval, step, $
           xp, yp, yp2
  
  
  ;; PLOT
  plot,xp,yp,psym=10,xtit='GLAT [deg]',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       thick=4,xr=[maxval,minval],/xst,yr=[0,1],tit='C > 0.1'
  axis,yaxis=1,yr=[0,100],ytit='N per '+string(step,format="(F0.2)")+$
       ' deg bin',/save,/yst
  oplot,xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6
  
  myps,/done,/mp
  
    
  ;;===========================================================================
  ;; PLOT 4 -- GRS success vs. BGPS flux & IRDC contrast (C >= 0.1)
  ;;=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  
  myps,'./irdc_dist_model/analysis_plots/grs_model_compare4.eps',$
       xsize=10,ysize=4.5
  multiplot,[2,1],xgap=0.06,/doxaxis,/doyaxis
  
  junk = where(rb3.grs_match EQ 1, nyes)
  print,'N making the cut: ',n_elements(rb3),'  N BAD: ',n_elements(rb3)-nyes
  print,'PLOT 4: Overall Success Rate = '+string(float(nyes)/n_elements(rb3)*$
                                                 100.,format="(F0.1,'%')")
  
  ;; Make bar chart -- IRDC Contrast
  ;; SR_BARS, array, minval, maxval, step, xp, yp, yp2
  minval = 0.d
  maxval = 0.5d
  step   = 0.05d
  sr_bars, rb3.c_meas, rb3.grs_match, minval, maxval, step, $
           xp, yp, yp2
  
  ;; PLOT
  plot,xp,yp,psym=10,xtit='IRDC Contrast',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       thick=4,xr=[0,maxval],/xst
  axis,yaxis=1,yr=[0,100],ytit='N per '+string(step,format="(F0.2)")+' bin',$
       /save,/yst
  oplot,xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6
  
  
  multiplot,/doxaxis,/doyaxis
  
  
  ;; Make bar chart -- BGPS FLUX DENSITY
  ;; SR_BARS, array, minval, maxval, step, xp, yp, yp2
  minval = -1.4d
  maxval = 0.5d
  step   = 0.1d
  sr_bars, alog10(rb3.s_peak/1.d3), rb3.grs_match, minval, maxval, step, $
           xp, yp, yp2
  
  ;; PLOT
  plot,10.^xp,yp,psym=10,xtit='BGPS 40" Flux Density [Jy]',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       thick=4,xr=10.^[minval,maxval],/xst,/xlog
  axis,yaxis=1,yr=[0,100],ytit='N per log(S)='+string(step,format="(F0.1)")+$
       ' bin',/save,/yst
  oplot,10.^xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6
  
  myps,/done,/mp
  
    
  ;;===========================================================================
  ;; PLOT 7 -- GRS success vs. BGPS flux & IRDC contrast (C >= 0.15)
  ;;=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  ic = WHERE(rb3.c_meas GE 0.15, nic)
  fullrb3 = rb3
  rb3 = rb3[ic]
  
  myps,'./irdc_dist_model/analysis_plots/grs_model_compare7.eps',$
       xsize=10,ysize=4.5
  multiplot,[2,1],xgap=0.06,/doxaxis,/doyaxis
  
  junk = where(rb3.grs_match EQ 1, nyes)
  print,'N making the cut: ',n_elements(rb3),'  N BAD: ',n_elements(rb3)-nyes
  print,'PLOT 7: Overall Success Rate = '+string(float(nyes)/n_elements(rb3)*$
                                                 100.,format="(F0.1,'%')")
  
  ;; Make bar chart -- IRDC Contrast
  ;; SR_BARS, array, minval, maxval, step, xp, yp, yp2
  minval = 0.d
  maxval = 0.5d
  step   = 0.05d
  sr_bars, rb3.c_meas, rb3.grs_match, minval, maxval, step, $
           xp, yp, yp2
  
  ;; PLOT
  plot,xp,yp,psym=10,xtit='IRDC Contrast',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       thick=4,xr=[0,maxval],/xst
  axis,yaxis=1,yr=[0,100],ytit='N per '+string(step,format="(F0.2)")+' bin',$
       /save,/yst
  oplot,xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6
    
  multiplot,/doxaxis,/doyaxis
  
  ;; Make bar chart -- BGPS FLUX DENSITY
  ;; SR_BARS, array, minval, maxval, step, xp, yp, yp2
  minval = -1.4d
  maxval = 0.5d
  step   = 0.1d
  sr_bars, alog10(rb3.s_peak/1.d3), rb3.grs_match, minval, maxval, step, $
           xp, yp, yp2
  
  ;; PLOT
  plot,10.^xp,yp,psym=10,xtit='BGPS 40" Flux Density [Jy]',$
       ytit='Success Rate',yst=8,xmargin=[10,10],$
       thick=4,xr=10.^[minval,maxval],/xst,/xlog
  axis,yaxis=1,yr=[0,100],ytit='N per log(S)='+string(step,format="(F0.1)")+$
       ' bin',/save,/yst
  oplot,10.^xp,yp2,psym=10,color=cgColor('BLK5'),linestyle=3
  
  al_legend,/top,/left,['Success Rate','N per bin'],linestyle=[0,3],$
            thick=[4,0],color=['Opposite','BLK5'],box=0,charsize=0.8,$
            linsize=0.6
  
  myps,/done,/mp
  
  
END
